package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Author: Zhihj
 * @Date: 2023-04-03 22:32:32
 */
@Service
public class ISysConfigServiceImpl implements ISysConfigService {
    public static final Logger log = LoggerFactory.getLogger(ISysConfigServiceImpl.class);
    @Autowired
    SysConfigMapper sysConfigMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 项目启动时。初始化参数到redis中
     */
    @PostConstruct
    public void init(){
        loadingConfigCache();
    }

    @Override
    public List<SysConfig> selectConfigList(SysConfig sysConfig) {

        return sysConfigMapper.selectConfigList(sysConfig);
    }

    @Override
    public void loadingConfigCache() {
        List<SysConfig> sysConfigs = sysConfigMapper.selectConfigList(new SysConfig());
        for (SysConfig sysConfig : sysConfigs) {
            //log.info("bianliang:{}","sys_config:"+sysConfig.getConfigKey());
            redisTemplate.opsForValue().set(getCacheKey(sysConfig.getConfigKey()),sysConfig.getConfigValue());
        }
    }
    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }
}
